package com.cgt.thymeleaf.controller;

import com.cgt.thymeleaf.pojo.Student;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {

	@RequestMapping("/index")
	public String index(Model model) {
		model.addAttribute("message", "HelloWorld");

		Student student = new Student(1, "李四", 23, new Date());
		model.addAttribute("student", student);

		Student student1 = new Student(1, "张三1", 23, new Date());
		Student student2 = new Student(2, "张三2", 23, new Date());
		Student student3 = new Student(3, "张三3", 23, new Date());
		List<Student> list = new ArrayList<>();
		list.add(student1);
		list.add(student2);
		list.add(student3);
		model.addAttribute("list", list);

		return "index";
	}

	@RequestMapping("/data")
	public String data(HttpServletRequest request, HttpSession session) {
		Student student1 = new Student(1, "张三1", 23 , new Date());
		request.setAttribute("student1", student1);

		Student student2 = new Student(2, "张三2", 23, new Date());
		session.setAttribute("student2", student2);

		Student student3 = new Student(3, "张三3", 23, new Date());
		ServletContext application = request.getServletContext();
		application.setAttribute("student3", student3);

		return "data";
	}
}
